Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
Javascript, replace "," with "." in JSON

I have as an input an invalid JSON string with this structure:

{
 "Seq_N":66,
 "Uptime":728,
 "Hum":33,500000,
 "Temp (C)":20,129999
}

I have no accesso to the code that produces the invalid JSON string so I can't fix the string before it's generated

In order to make the JSON string valid, I need to replace the comma character inside the Hum and Temp fields with a dot character, but I also need to keep the comma separators of the JSON string (in order not to break the JSON syntax)

Does anyone know a clean function/regex to do this?

Thanks in advance

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You can use regex like this:

var json = `{
 "Seq_N":66,
 "Uptime":728,
 "Hum":33,500000,
 "Temp (C)":20,129999
}`;

json = json.replace(/("(Hum|Temp \(C\))":\d+),/g, '$1.');

console.log(json)
about 4 years ago · Santiago Gelvez Relatório

0

Negative lookahead would do the job.

const foo = '{ "Seq_N":66, "Uptime":728, "Hum":33,500000, "Temp (C)":20,129999 }';

const cleanedFoo = foo.replace(/,(?![" ])/g, ".");

console.log(JSON.parse(cleanedFoo));

The regex means:

, match comma,

(?! only if it's not followed

[" ] by either double quotes or space. (Yes, this so-called JSON is seriously malformed).

about 4 years ago · Santiago Gelvez Relatório

0

If I understand the question correctly, the following code should do the job:

$(".stat-item").each(function () {
var inner_context = $(this).text();
$(this).text(inner_context.replace(/[\.]+/g, ","));
});
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda